using AnalyStock.DataManage;

namespace AnalyStock.GrapImage;

/// <summary>
///     初始化鼠标菜单，添加或删除自选股
/// </summary>
public partial class ChartKline : UserControl
{
    /// <summary>
    /// 自选股菜单处理，自选股概念记录面板
    /// </summary>
    private async void InitializeMenu_SeleStockNote()
    {
        AddSeleStockNotePanel.InitializeMouseDarg(this);
        AddSeleStockNotePanel.Visibility = Visibility.Hidden;
        BtnAddStockNote.Click += (s, e) =>
       {
           UpdateSeleStock(s, e);
           AddSeleStockNotePanel.Visibility = Visibility.Hidden;
       };
        BtnCancelStockNote.Click += (s, e) =>
        {
            TextBoxStockNote.Text = string.Empty;
            AddSeleStockNotePanel.Visibility = Visibility.Hidden;
        };
        await FillSeleStockMenu();
    }
    private async Task FillSeleStockMenu()
    {
        ContextMenu contextMenu = new()
        {
            Style = SetElementStyle("ContextMenuStyle")
        };
        //子菜单：自选股添加
        var _conceptions = await FillConceptionAsync().ConfigureAwait(false);

        MenuItem menuItem_Add = new()
        {
            Header = "🔸添加自选股",
            ToolTip = "▷",
            Name = "Add",
            Style = SetElementStyle("MenuItemStyle")
        };
        if (!_conceptions.IsNullOrEmpty())
        {
            _conceptions.ForEachDo(item =>
            {
                MenuItem menuItem_Conception = new()
                {
                    Header = item.Name,
                    ToolTip = item.OrderID,
                    Name = "Add",
                    Style = SetElementStyle("MenuItemStyle")
                };
                menuItem_Conception.Click += UpdateSeleStock;
                menuItem_Add.Items.Add(menuItem_Conception);
            });
        }
        //子菜单：删除自选股
        MenuItem menuItem_Del = new()
        {
            Header = "🔸删除自选",
            Name = "Dele",
            Style = SetElementStyle("MenuItemStyle")
        };
        menuItem_Del.Click += UpdateSeleStock;
        //子菜单：添加概念分类
        MenuItem menuItem_Classify = new()
        {
            Header = "🔸概念分类",
            Name = "Classify",
            Style = SetElementStyle("MenuItemStyle")
        };
        menuItem_Classify.Click += (s, e) =>
            ConceptionClassifyPanel.Visibility = Visibility.Visible;
        //子菜单：添加自选股的产品、概念题材说明
        MenuItem menuItem_StockNote = new()
        {
            Header = "🔸股票说明",
            Name = "Note",
            Style = SetElementStyle("MenuItemStyle")
        };
        menuItem_StockNote.Click += (s, e) =>
        {
            TextBoxStockNote.Text = currStockInfo.Note;
            AddSeleStockNotePanel.Visibility = Visibility.Visible;
        };
        contextMenu.Items.Add(menuItem_Add);
        contextMenu.Items.Add(menuItem_Del);
        contextMenu.Items.Add(menuItem_Classify);
        contextMenu.Items.Add(menuItem_StockNote);
        ContextMenu = contextMenu;
    }

    //自选股或分类概念更新后，触发以实现其他窗口相关数据的更新
    internal event EventHandler<SelectStock> Updated_SeleStock;
    protected void OnSeleStockChanged(SelectStock sele) =>
        Updated_SeleStock?.Invoke(null, sele);

    private async void UpdateSeleStock(object sender, RoutedEventArgs e)
    {
        var currentItem = sender as MenuItem;
        if (currStockInfo is null)
        {
            return;
        }

        var seleStocksFromDb = await FillSeleStocksAsync();
        var currSeleStock = seleStocksFromDb?.FirstOrDefault(
            n => n.Ts_code.Equals(currStockInfo.Ts_code));
        if (currSeleStock is SelectStock)
        {
            currSeleStock.Note = TextBoxStockNote.Text.Trim();
        }
        else
        {
            currSeleStock = new SelectStock
            {
                Ts_code = currStockInfo.Ts_code,
                Name = currStockInfo.Name,
                Industry = currStockInfo.Industry,
                Conception = (string)currentItem.Header,
                Note = TextBoxStockNote.Text.Trim()
            };
        }

        switch (currentItem)
        {
            case { Name: "Add" }:
                await CommDataMethod<SelectStock>.UpdateSeleStockAsync(new List<SelectStock> { currSeleStock });
                currStockInfo.Issele = true;
                currStockInfo.Conception = currSeleStock.Conception;
                break;
            case { Name: "Dele" }:
                if (!"是否确认删除该自选股?".IsConfirmMessage())
                {
                    return;
                }

                await CommDataMethod<SelectStock>.DelSeleStockAsync(new List<SelectStock> { currSeleStock });
                currStockInfo.Issele = false;
                currStockInfo.Conception = string.Empty;
                break;
            default: //save 的 button
                if (!currStockInfo.Issele)
                {
                    "请保存为自选股，再注释说明...".ErrorMessage();
                    return;
                }

                if (!"是否确认保存注释?".IsConfirmMessage())
                {
                    return;
                }

                await CommDataMethod<SelectStock>.UpdateSeleStockAsync(new List<SelectStock> { currSeleStock });
                currStockInfo.Note = currSeleStock.Note;
                break;
        }

        seleStocksFromDb.Dispose();
        //改变面板股票名称、代码提示
        TextHeaderItem();
        //传递股票改变状态，传播事件
        OnSeleStockChanged(currSeleStock);
    }
}